package com.woniu.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.R;
import com.woniu.car.bean.TCar;
import com.woniu.car.bean.TCarrier;
import com.woniu.car.bean.TDriver;
import com.woniu.car.service.ITCarService;
import com.woniu.car.service.ITCarrierService;
import com.woniu.car.service.ITDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kk
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/driver")
@CrossOrigin
public class TDriverController {

    @Autowired
    private ITDriverService driverService;

    @Autowired
    private ITCarrierService carrierService;

    @Autowired
    private ITCarService carService;

    @GetMapping
    public R selectAll(Integer currentPage, Integer pageSize){
        R r =null;

        //分页条件约束
        IPage<TDriver> iPage = new Page<>(currentPage,pageSize);
        IPage<TDriver> page = driverService.page(iPage);
        long total = page.getTotal();
        List<TDriver> records = page.getRecords();
        //将货运商对象、车对象装入司机对象中
        records.forEach(e->{
            TCarrier byId = carrierService.getById(e.getCarrierId());
            e.setCarrier(byId);
            TCar car = carService.getById(e.getCarId());
            e.setCar(car);

        });
        System.out.println(records);
        Map map=new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        r=R.OK();
        r.setData(map);
        return r;

    }
    //删除
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id){
//        System.out.println(id);
        boolean b = driverService.removeById(id);
        R r =null;
        if(b){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    //新增
    @PostMapping
    public R saveDriver(@RequestBody TDriver TDriver){
        R r =null;
        String driverName = TDriver.getDriverName();
        String driverPhone = TDriver.getDriverPhone();
        List<TDriver> list = driverService.list();
        //查询是否有重复数据
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getDriverPhone().equals(driverPhone)&&list.get(i).getDriverName().equals(driverName)){
                r=R.ERROR();
                return r;
            }
        }

        boolean b = driverService.save(TDriver);
        if(b){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    //修改
    @PutMapping
    public R update(@RequestBody TDriver TDriver){
        boolean b = driverService.updateById(TDriver);
        R r =null;
        if(b){
            r=R.OK();
        } else {
            r=R.ERROR();
        }
        return r;
    }

    //根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        TDriver TDriver = driverService.getById(id);
        R r= R.OK();
        r.setData(TDriver);
        return r;
    }

}
